package com.dingyuhang.bean;


import com.dingyuhang.common.TiebaException;
import com.dingyuhang.enums.Gender;
import com.dingyuhang.enums.Role;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;

import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

/**
 * 
 *
 * @date 2021年1月23日
 * @todo 用户
 * @project_name bawei_tieba
 */
public class User implements Serializable{
	

	

	private static final long serialVersionUID = 4376983832642067436L;
	/**
	 * 主键
	 */
	private Integer id;
	/**
	 * 用户名
	 */
	@NotBlank(message="用户名不能为空")
	@Size(min = 2, message = "用户名必须2个字母以上")
	private String userName;
	/**
	 * 用户密码
	 */
	@NotBlank(message="密码不能为空")
	@Size(min = 6, message = "密码必须6位以上")
	private String password;
	
	/**
	 * 老密码
	 */
	@NotBlank(message="老密码不能为空")
	private String oldPassword;
	/**
	 * 用户昵称
	 */
	
	private String nickName;
	/**
	 * 性别
	 */
	private Gender gender;
	/**
	 * @DateTimeFormat(pattern="yyyy-MM-dd") 接收页面的日期格式转换 否则回报400错误
	 * 出生日期
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date birthday;
	/**
	 * 角色 0普通用户  1管理员
	 */
	private Role role;
	/**
	 * 注册日期
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd")
	private Date created;
	
	/**
	 * 用户的锁定状态
	 */
	private Integer locked;
	
	/**
	 * 用户年龄
	 */
	private Integer age;
	
	
	//扩展用户的帖子数量
	private Integer topicNum;
	
	
	//重复密码
	private String  repassword;
	
	//三级联动的扩展字段
	private City province; //省
	
	private City city;//市
	
	private City county;//市
	
	
	private String imgurl;
	
	
	
	public void setTopicNum(Integer topicNum) {
		this.topicNum = topicNum;
	}
	
	public Integer getTopicNum() {
		return topicNum;
	}
	
	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public User(Integer locked, Integer id, String userName, String password, String nickmName, Gender gender,
			Date birthday, Role role, Date created, Integer topicNum) {
		super();
		this.locked = locked;
		this.id = id;
		this.userName = userName;
		this.password = password;
		this.nickName = nickmName;
		this.gender = gender;
		this.birthday = birthday;
		this.role = role;
		this.created = created;
		this.topicNum = topicNum;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getNickName() {
		return nickName;
	}

	public void setNickName(String nickName) {
		this.nickName = nickName;
	}

	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	
	public Integer getLocked() {
		return locked;
	}

	public void setLocked(Integer locked) {
		this.locked = locked;
	}
	

	
	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}
	
	public City getProvince() {
		return province;
	}

	public void setProvince(City province) {
		this.province = province;
	}
	

	public String getImgurl() {
		return imgurl;
	}

	public void setImgurl(String imgurl) {
		this.imgurl = imgurl;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public City getCounty() {
		return county;
	}

	public void setCounty(City county) {
		this.county = county;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "User [locked=" + locked + ", id=" + id + ", userName=" + userName + ", password=" + password
				+ ", nickName=" + nickName + ", gender=" + gender + ", birthday=" + birthday + ", role=" + role
				+ ", created=" + created + ", topicNum=" + topicNum + "]";
	}
	
	
	   public void validatePassword(){
	        if(this.repassword == null || !this.repassword.equals(this.password)){
	            throw new TiebaException("两次密码不一致");
	        }
	    }

	   public void validateCity (){
	        if(this.county.getId() == null || this.county.getId() ==0){
	            throw new TiebaException("三级联动省市县必选");
	        }
	    }
	
	
	
	
}
